﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class NguoiQuanLyDAO
    {
        public static DataTable KTDangNhap(NguoiQuanLyDTO ql)
        {
            SqlConnection cn = DataProvider.Connection();
            string sql = "select * from NGUOIQUANLY where TenDangNhap = @TenDN and MatKhau = @MK";
            SqlDataAdapter da = new SqlDataAdapter(sql, cn);
            da.SelectCommand.Parameters.Add("@TenDN", SqlDbType.VarChar).Value = ql.TenDN;
            da.SelectCommand.Parameters.Add("@MK", SqlDbType.VarChar).Value = ql.MKhau;
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
        public static int DoiMatKhau(string mk,string mkcu)
        {
            SqlConnection conn = DataProvider.Connection();
            conn.Open();
            string sql = "usp_DoiMK";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@MK", SqlDbType.NVarChar).Value = mk;
            cmd.Parameters.Add("@MKcu", SqlDbType.NVarChar).Value = mkcu;
            cmd.Parameters.Add("@k", SqlDbType.Int);
            cmd.Parameters["@k"].Direction = ParameterDirection.Output;
            cmd.ExecuteNonQuery();
            int k = Int32.Parse(cmd.Parameters["@k"].Value.ToString());
            if (k == 1)
                return 1;
            else
                return 0;
        }
        public static void BackUp(string path)
        {
            SqlConnection conn = DataProvider.Connection();
            conn.Open();
            string sql = "usp_Backup";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@path", SqlDbType.NVarChar).Value = path;
            cmd.ExecuteNonQuery();
        }
        public static void Restore(string path)
        {
            SqlConnection conn = DataProvider.Connection();
            conn.Open();
            SqlCommand cm = new SqlCommand();
            cm.Connection = conn;
            cm.CommandText = "use master";
            cm.ExecuteNonQuery();
            cm.CommandText = "if exists (select name from sys.databases where name = N'PinMatTroi') begin drop database PinMatTroi end";
            cm.ExecuteNonQuery();
            cm.CommandText = "restore database PinMatTroi from disk ='" + path + "'";
            cm.ExecuteNonQuery();
        }
    }
}
